<!-- 角色管理 -->
<template>
  <div class="app-container" id='adminRole'>
    <!-- 查询以及其他操作 -->
    <div class="filter-container" style="float: right;">
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入角色名称">
      </el-input>
      <el-button type="primary" class="filter-item" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button type="primary" class="filter-item" icon="el-icon-edit" @click="handleCreate">新增</el-button>
    </div>
    <!-- 查询结果，表格功能 -->
    <!-- <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="date" label="日期" width="180">
      </el-table-column>
      <el-table-column prop="name" label="姓名" width="180">
      </el-table-column>
      <el-table-column prop="address" label="地址">
      </el-table-column>
    </el-table> -->
    <el-table :data="tableData" style="width: 100%" border fit highlight-current-row>
      <el-table-column prop="name" align="center" label="角色名称" sortable>
      </el-table-column>
      <el-table-column prop="desc" align="center" label="说明">
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="primary" size="small">编辑</el-button>
          <el-button @click="handleDelete(scope.row)" type="danger" size="small">删除</el-button>
          <el-button @click="handlePermission(scope.row)" type="primary" size="small">授权</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
      @pagination="getList" />
    <!-- <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog> -->
    <!-- 新增、修改模态框 -->
    <el-dialog :title="dialogStatus" :visible.sync="dialogFromVisible">
        <el-form ref="dataForm" :rules="rules" :model="dataForm"  label-width="100px" size="mini" style="width: 400px;margin-left: 20px;">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="dataForm.name"></el-input>
          </el-form-item>
          <el-form-item label="说明" prop="desc">
            <el-input v-model="dataForm.desc"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFromVisible = false">取消</el-button>
          <el-button v-if="dialogStatus === '新增'" type="primary" @click="createData">确定</el-button>
          <el-button v-else type="primary" @click="updateData">确定</el-button>
        </div>
      </el-dialog>
      <!-- 权限配置模态框 -->
      <el-dialog title="权限配置" :visible.sync="permissionDialogFormVisible">
        <el-tree
            ref="tree"
            :data="systemPermissions"
            :default-checked-keys="assignedPermissions"
            show-checkbox
            node-key="id"
            highlight-current
            :props="defaultProps">
            <!-- <span slot-scope="{node,data}" class="custom-tree-node">
               <span>{{data.label}}</span>
            </span> -->
        </el-tree>
        <div slot="footer" class="dialog-footer">
            <el-button @click="permissionDialogFormVisible = false">取消</el-button>
            <el-button type="primary" @click="updatePermission">确定</el-button>
        </div>
      </el-dialog>
  </div>
</template>
<script >
import Pagination from '@/components/Pagination'
import {listAdminRole,createAdminRole,updateAdminRole,deleteAdminRole,getPermissions,updatePermission} from "@/api/system/adminRole"
export default {
    name: 'adminRole',
    components: {
      Pagination
    },
    computed: {

    },
    data() {
      return {
        //查询
        listQuery: {
          page: 1,
          limit: 10,
          name: ''
        },
        //表格数据
        tableData: [],
        //总条数
        total: 0,
        //新增、修改标题状态
        dialogStatus: '新增',
        //模态框状态值
        dialogFromVisible: false,
        //表单提交数据
        dataForm: {
            id: '',
            name: '',
            desc: ''
        },
        //表单校验
        rules: {
            name: [
                {required: true,message: '角色名称不能为空',trigger: 'blur'}
            ]
        },
        //权限配置提交表单
        permissionForm: {
            roleId: '',
            permissions: []
        },
        //权限配置模态框状态
        permissionDialogFormVisible: false,
        //权限配置树结构数据
        systemPermissions: [],
        //所勾选的树节点数据
        assignedPermissions: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
      }
    },
    created() {
      // 数据初始化
      this.getList();
    },
    methods: {
      //获取表格数据
      getList() {
        listAdminRole(this.listQuery).then(res => {
          this.tableData = res.data.data.list;
          this.total = res.data.data.total;
        })
      },
      //查询
      handleFilter() {
        this.listQuery.page = 1;
        this.getList();
      },
      //重置表单
      resetForm(){
        this.dataForm = {
            id: '',
            name: '',
            desc: ''
        }
      },
      //新增
      handleCreate() {
        this.resetForm();
        this.dialogStatus = '新增';
        this.dialogFromVisible = true;
      },
      //确定新增
      createData(){
        this.$refs['dataForm'].validate(valid => {
            if(valid){
              createAdminRole(this.dataForm).then(res => {
                    this.getList();
                    this.dialogFromVisible = false;
                    this.$notify.success({
                        title: '成功',
                        message: '新增角色成功'
                    })
                }).catch(res => {
                    this.$notify.error({
                        title: '失败',
                        message: res.data.errmsg
                    })
                })
            }
        })
      },
      //编辑
      handleUpdate(row) {
         this.dataForm = Object.assign({},row);
         this.dialogStatus = '修改';
         this.dialogFromVisible = true;
      },
      //确定修改
      updateData(){
        this.$refs['dataForm'].validate(valid => {
            if(valid){
              updateAdminRole(this.dataForm).then(res => {
                    this.getList();
                    this.dialogFromVisible = false;
                    this.$notify.success({
                        title: '成功',
                        message: '修改角色成功'
                    })
                }).catch(res => {
                    this.$notify.error({
                        title: '失败',
                        message: res.data.errmsg
                    })
                })
            }
        })
      },
      //删除
      handleDelete(row) {
        this.$confirm('此操作将删除该管理员，是否继续？','提示',{
            confirmButtonText: '确定',
            concelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            deleteAdminRole(row).then(res => {
              this.getList();
              this.$notify.success({
                title: '成功',
                message: '删除成功'
              })
            })
          }).catch(() => {
             this.$message({
               type: 'info',
               message: '已取消删除'
             })
          })
      },      
      //授权
      handlePermission(row) {
        this.permissionDialogFormVisible = true;
        this.permissionForm.roleId = row.id
        //获取权限信息
        getPermissions({roleId: row.id}).then(res =>{
           this.systemPermissions = res.data.data.systemPermissions;
           this.assignedPermissions = res.data.data.assignedPermissions;
        })
      },
      //确定更新授权
      updatePermission(){
          console.log(this.$refs.tree.getCheckedKeys(true))
          this.permissionForm.permissions = this.$refs.tree.getCheckedKeys(true);
          updatePermission(this.permissionForm).then(res => {
            this.getList();
            this.permissionDialogFormVisible = false;
            this.$notify.success({
                title: '成功',
                message: '权限更新成功'
            })
          })
      }
    }
  }

</script>
<style>
</style>
<style scoped>
</style>